<?php defined('BASEPATH') OR exit('No direct script access allowed');

class ranking extends Services{
	private $status = true;
	
	function ranking(){
		parent::__construct();
	}
	
	/**
	 * Calificar series o peliculas
	 */
	public function rating(){
		$this->checkAccessToken();
		
		$response_errors = $this->validate('rating');
		if($this->status == true){
			$this->load->model('ranking_model');
			$data = $this->ranking_model->rating(self::$params);
			if($data)
				self::$msg_response = 'txt_rating_added';
			else
				self::$msg_response = 'txt_qualify_failed';
		}else
			throw new UserValidateException(implode(',',$response_errors));
	}
	
	
	
	
	
	/**
     * Permite incorporar las validaciones bases que apliquen para todos los servicios.
     * Validaciones mas espesificas tendran que ser en cada servicio en particular.
     * @author gama
     */
    private function validate($val_method){
    	if(method_exists($this, 'validate_'.$val_method)){
    		$this->load->library('MY_Form_validation');
    		return $this->{'validate_'.$val_method}();
    	}
    }
    
	private function validate_rating(){
    	$validate = new MY_Form_validation();
    	$validate->setArrayValidate(self::$params);
    	
    	$validate->set_rules(array(
    			array('field' => 'id', 
                     'label' => 'id', 
                     'rules' => 'required|is_natural_no_zero'),
				array('field' => 'type', 
                     'label' => 'type', 
                     'rules' => 'required|expression:/^(film|episode)$/'),
				array('field' => 'rating', 
                     'label' => 'rating', 
                     'rules' => 'required|numeric')
    	));
		$this->status = $validate->run();
		
		$fecha = new Fecha();
		if(isset(self::$params['date_release'])){
			if(!$fecha->is_date(self::$params['date_release'])){
				$this->status = FALSE;
				$validate->_error_array['date_release'] = lang('txt_invalid_date').' (date_release)';
			}
		}
		
		
    	return $validate->_error_array;
    }

    
    
    
	private function parseOutput($data=array(), $only_parse=false){
		if ($only_parse)
			return $this->{'_format_'.$this->getFormat()}($data);
		if($this->status==true){
			return $this->{'_trim_format_'.$this->getFormat()}(
					$this->{'_format_'.$this->getFormat()}($data)
					);
		}
    }
}